<?php

namespace app\api\model;

use app\common\model\Region;
use app\common\model\People as PeopleModel;

/**
 * 用户收货地址模型
 * Class UserAddress
 * @package app\common\model
 */
class People extends PeopleModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'wxapp_id',
        'create_time',
        'update_time'
    ];

    /**
     * @param $user_id
     * @return false|static[]
     * @throws \think\exception\DbException
     */
    public function getList($user_id)
    {
//        return self::all(compact('user_id'));
        $list = $this
            ->with(['image.file'])
            ->where('user_id', '=', $user_id)
            ->select();
        return $list;
    }

    /**
     * 新增
     * @param User $user
     * @param $data
     * @return bool
     * @throws \think\exception\PDOException
     */
    public function add($user, $data)
    {
        //工作城市信息
        $region = explode(',', $data['region']);
        $province_id = Region::getIdByName($region[0], 1);
        $city_id = Region::getIdByName($region[1], 2, $province_id);
        $region_id = Region::getIdByName($region[2], 3, $city_id);

//        $work_city = $province_id.','.$city_id.','.$region_id;

        //家乡城市信息
        $home = explode(',', $data['home']);
        $province_two_id = Region::getIdByName($home[0], 1);
        $city_two_id = Region::getIdByName($home[1], 2, $province_two_id);
        $region_two_id = Region::getIdByName($home[2], 3, $city_two_id);

//        $home_city = $province_two_id.','.$city_two_id.','.$region_two_id;

        // 添加收货地址
        $this->startTrans();
        try {
            $this->allowField(true)->save([
                'name' => $data['name'],
                'sex' => $data['sex'],
                'birthday' => $data['birthday'],
                'height' => $data['height'],
                'weight' => $data['weight'],
                'education' => $data['education'],
                'work' => $data['work'],
                'income' => $data['income'],
                'is_house' => $data['is_house'],
                'is_car' => $data['is_car'],
                'work_province_id' => $province_id,
                'work_city_id' => $city_id,
                'work_region_id' => $region_id,

                'home_province_id' => $province_two_id,
                'home_city_id' => $city_two_id,
                'home_region_id' => $region_two_id,

                'work_city' => $data['region'],
                'home_city' => $data['home'],
                'marriage' => $data['marriage'],
                'description' => $data['description'],
                'demand' => $data['demand'],
                'user_id' => $user['user_id'],
                'wxapp_id' => self::$wxapp_id
            ]);
            // 商品图片
            $images = explode(',',$data['images']);
            $this->addPeopleImages($images);
            // 设为默认收货地址
            !$user['people_id'] && $user->save(['people_id' => $this['id']]);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }

    /**
     * 编辑
     * @param $data
     * @return false|int
     */
    public function edit($data)
    {
        // 添加收货地址
        /*$region = explode(',', $data['region']);
        $province_id = Region::getIdByName($region[0], 1);
        $city_id = Region::getIdByName($region[1], 2, $province_id);
        $region_id = Region::getIdByName($region[2], 3, $city_id);
        return $this->allowField(true)->save([
            'name' => $data['name'],
            'phone' => $data['phone'],
            'province_id' => $province_id,
            'city_id' => $city_id,
            'region_id' => $region_id,
            'detail' => $data['detail'],
            'district' => ($region_id === 0 && !empty($region[2])) ? $region[2] : '',
        ]);*/
        //工作城市信息
        $region = explode(',', $data['region']);
        $province_id = Region::getIdByName($region[0], 1);
        $city_id = Region::getIdByName($region[1], 2, $province_id);
        $region_id = Region::getIdByName($region[2], 3, $city_id);
        //家乡城市信息
        $home = explode(',', $data['home']);
        $province_two_id = Region::getIdByName($home[0], 1);
        $city_two_id = Region::getIdByName($home[1], 2, $province_two_id);
        $region_two_id = Region::getIdByName($home[2], 3, $city_two_id);
//        return $data;
        $res = $this->where(['people_id' => $data['people_id']])->update([
            'name' => $data['name'],
            'sex' => $data['sex'],
            'birthday' => $data['birthday'],
            'height' => $data['height'],
            'weight' => $data['weight'],
            'education' => $data['education'],
            'work' => $data['work'],
            'income' => $data['income'],
            'is_house' => $data['is_house'],
            'is_car' => $data['is_car'],
            'work_province_id' => $province_id,
            'work_city_id' => $city_id,
            'work_region_id' => $region_id,

            'home_province_id' => $province_two_id,
            'home_city_id' => $city_two_id,
            'home_region_id' => $region_two_id,

            'work_city' => $data['region'],
            'home_city' => $data['home'],
            'marriage' => $data['marriage'],
            'description' => $data['description'],
            'demand' => $data['demand'],
            'update_time' => time()
        ]);
        // 商品图片
        $images = explode(',',$data['images']);
        $this->editPeopleImages($images,$data['people_id']);
        return $res;
    }

    /**
     * 添加图片
     * @param $images
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    private function addPeopleImages($images)
    {
        $this->image()->delete();
        $data = array_map(function ($image_id) {
            return [
                'image_id' => $image_id,
                'wxapp_id' => self::$wxapp_id
            ];
        }, $images);
        return $this->image()->saveAll($data);
    }

    private function editPeopleImages($images,$people_id){
        $peopleImage = new PeopleImage();
        $peopleImage -> where('people_id','=',$people_id)->delete();
        $data = [];
        foreach($images as $k=>$v){
            $data[$k]['people_id'] = (int)$people_id;
            $data[$k]['image_id'] = (int)$v;
            $data[$k]['wxapp_id'] = (int)self::$wxapp_id;
            $data[$k]['create_time'] = time();
        }
        return $peopleImage->saveAll($data);

    }



    /**
     * 设为默认收货地址
     * @param null|static $user
     * @return int
     */
    public function setDefault($user)
    {
        // 设为默认地址
        return $user->save(['address_id' => $this['address_id']]);
    }

    /**
     * 删除收货地址
     * @param null|static $user
     * @return int
     */
    public function remove($user)
    {
        // 查询当前是否为默认地址
        $user['address_id'] == $this['address_id'] && $user->save(['address_id' => 0]);
        return $this->delete();
    }

    /**
     * 收货地址详情
     * @param $user_id
     * @param $people_id
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($user_id, $people_id)
    {
//        return self::get(compact('user_id', 'people_id'),['image.file']);
        return self::get(compact('user_id', 'people_id'),['image.file']);
    }

}
